#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

class solution
{
public:
    map<string, int> WordRecognition(string A)
    {
        vector<string> WordGroups;
        auto it = A.begin();
        string tmparr;
        while (it != A.end())
        {
            tmparr.clear();
            while (it != A.end() - 1 && *it != ' ')
            {
                if (*it >= 57 && *it <= 90)
                {
                    tmparr.push_back(*it + 32);
                    it++;
                }
                else
                {
                    tmparr.push_back(*it);
                    it++;
                }
            }
            if (!tmparr.empty())
                WordGroups.push_back(tmparr);
            else
                it++;
        }
        map<string, int> CountMap;
        for (const auto& it : WordGroups)
        {
            CountMap[it]++;
        }
        auto print = CountMap.begin();
        while (print != CountMap.end())
        {
            cout << print->first << ':' << print->second << endl;
            print++;
        }
        return CountMap;
    }
};

class Solution2 {
public:
    struct compare
    {
        bool operator()(const pair<string, int>& a, const pair<string, int>& b)
        {
            return a.second > b.second;
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k)
    {
        map<string, int> countmap;
        for (const auto& it : words)
        {
            countmap[it]++;
        }
        vector<pair<string, int>> kvvec(countmap.begin(), countmap.end());
        stable_sort(kvvec.begin(), kvvec.end(), compare());
        vector<string> ret;
        for (int i = 0; i < k; i++)
        {
            ret.push_back(kvvec[i].first);
        }
        return ret;
    }
};

int main()
{
    string A("A blockhouse is a small castle that has four openings through which to shoot.");
    //string A("A blockhouse is a.");

    solution B;
    B.WordRecognition(A);
    return 0;
}